package com.plian.system.controller.fr.enterprise;

import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.fr.enterprise.Enterprise;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.fr.enterprise.IEnterpriseService;
import com.plian.system.vo.fr.enterprise.EnterpriseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping(value = "api/enterprise")
@Api(value = "EnterpriseController", tags = "企业信息 控制器")
public class EnterpriseController extends BaseController {

    private final IEnterpriseService enterpriseService;

    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入 enterprise")
    public R<EnterpriseVO> detail(@RequestBody Enterprise enterprise) {
        EnterpriseVO enterpriseVO = enterpriseService.getDetailObj(enterprise);
        return R.data(enterpriseVO);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<EnterpriseVO> list(@RequestBody Query query) {
        return enterpriseService.list(query);
    }

    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @LogOperation("企业信息-提交")
    @ApiOperation(value = "新增或修改", notes = "传入 tripleEvent")
    public R<Enterprise> submit(@Valid @RequestBody Enterprise enterprise) {
        boolean result = enterpriseService.submit(enterprise);
        return result ? R.data(enterprise) : new R(CommonCode.FAIL);
    }

    @PostMapping("/remove")
    @LogOperation("企业信息-删除")
    @ApiOperation(value = "删除", notes = "传入 ids")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return enterpriseService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    @PostMapping(value = "/reportList")
    @ApiOperation(value = "报表", notes = "传入 pd")
    public ListResult reportList(@RequestBody PageData pd){
        return enterpriseService.reportList(pd);
    }

    @PostMapping(value = "/findByOrgId")
    @ApiOperation(value = "根据组织id获取企业信息", notes = "传入 orgId")
    public R<EnterpriseVO> findByOrgId(@RequestBody PageData pd){
        return R.data(enterpriseService.findByOrgId(pd.getString("orgId")));
    }
}
